package com.viking.XFT;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;

import java.util.ArrayList;


public class FloatMain extends Activity {
    static final String TAG = "FloatMain";
    static final String PREF_NAME = "XFT";
    static final String PREF_KEY_SOB = "start_on_boot";
    static final int RESULT_ENABLE = 1;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.float_main);

        View.OnClickListener click_listener = new ButtonsClickListener();

        findViewById(R.id.button_start).setOnClickListener(click_listener);
        findViewById(R.id.button_stop).setOnClickListener(click_listener);

        CheckBox cb = (CheckBox)findViewById(R.id.checkbox_start_on_boot);
        cb.setChecked(getSharedPreferences(PREF_NAME, MODE_PRIVATE).getBoolean(PREF_KEY_SOB, false));
        cb.setOnClickListener(click_listener);
    }

    private class ButtonsClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button_start:
                    if (!isWorked()) {
                        Intent intent = new Intent(FloatMain.this, FloatService.class);
                        startService(intent);
                    }
                    finish();
                    break;

                case R.id.button_stop:
                    Intent intent = new Intent(FloatMain.this, FloatService.class);
                    stopService(intent);
                    removeAdminComp();
                    finish();
                    break;

                case R.id.checkbox_start_on_boot:
                    SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
                    editor.putBoolean(PREF_KEY_SOB, ((CheckBox)v).isChecked());
                    editor.commit();
                    break;

                default:
                    break;
            }
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        addAdminComp();
    }

    private void addAdminComp() {
        // Admin init
        Log.d(TAG, "Admin init");
        DevicePolicyManager device_policy_manager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName admin_component = new ComponentName(this.getPackageName(), XFTDeviceAdminReceiver.class.getName());
        if (!device_policy_manager.isAdminActive(admin_component)) {
            Log.d(TAG, "Admin add");
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin_component);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, R.string.request_for_lock_screen);
            startActivityForResult(intent, RESULT_ENABLE);
        }
    }

    private void removeAdminComp() {
        Log.d(TAG, "Admin init");
        DevicePolicyManager device_policy_manager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName admin_component = new ComponentName(this.getPackageName(), XFTDeviceAdminReceiver.class.getName());
        device_policy_manager.removeActiveAdmin(admin_component);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case RESULT_ENABLE:
                Log.i(TAG, resultCode==Activity.RESULT_OK ? "Admin enabled" : "Admin enable failed");
                return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    public boolean isWorked()
    {
        ActivityManager myManager=(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServiceInfo>) myManager.getRunningServices(50);
        for (RunningServiceInfo aRunningService : runningService) {
            if (aRunningService.service.getClassName().equals("com.viking.XFT.FloatService")) {
                return true;
            }
        }
        return false;
    }
}